package httpbot;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import com.google.common.base.Preconditions;

public class HttpRequests implements IRequests{

	@Override
	public String getPageSource(String title) {
		Preconditions.checkNotNull(title);
		Preconditions.checkArgument(title.length() > 0, "Paramater must have a lenght");
		RequestBuilder builder = new RequestBuilder();
		builder.setAction("query");
		builder.setProp("revisions");
		builder.setRvprop("content");
		builder.setFormat("json");
		builder.setTitles(title);
		
		BufferedReader reader = null;
		StringBuilder stringBuilder = new StringBuilder(1000);
		try{
			URL url = new URL(builder.create());
			reader = new BufferedReader(new InputStreamReader(url.openStream()));
			String line = null;
			while ((line = reader.readLine()) != null){
				stringBuilder.append(line);
				stringBuilder.append("\n");
			}
		} 
		catch (MalformedURLException e){
			e.printStackTrace();
		} 
		catch (IOException e){
			e.printStackTrace();
		}
		finally{
			if (reader != null){
				try{
					reader.close();
				}
				catch (IOException e){
					e.printStackTrace();
				}
			}
		}
		
		return stringBuilder.toString();
	}

	
	private class RequestBuilder{
		
		private static final String BASE_VALUE = "http://en.wikipedia.org/w/api.php?";
		
		private static final String SPLIT = "&";
		private static final String ACTION = "action=";
		private static final String PROP = "prop=";
		private static final String RVPROP = "rvprop=";
		private static final String FORMAT = "format=";
		private static final String TITLES = "titles=";
		
		private int fieldsAdded;
		
		private StringBuilder request;
		
		public RequestBuilder(){
			request = new StringBuilder();
			request.append(BASE_VALUE);
			fieldsAdded = 0;
		}
		
		public void clear(){
			request = new StringBuilder();
			request.append(BASE_VALUE);
			fieldsAdded = 0;
		}
		
		public String create(){
			return request.toString();
		}
		
		public void setAction(String action){
			if (fieldsAdded > 0){
				request.append(SPLIT);
			}
			request.append(ACTION);
			request.append(action);
			fieldsAdded++;
		}
		
		public void setProp(String prop){
			if (fieldsAdded > 0){
				request.append(SPLIT);
			}
			request.append(PROP);
			request.append(prop);
			fieldsAdded++;
		}
		
		public void setRvprop(String rvprop){
			if (fieldsAdded > 0){
				request.append(SPLIT);
			}
			request.append(RVPROP);
			request.append(rvprop);
			fieldsAdded++;
		}
		
		public void setFormat(String format){
			if (fieldsAdded > 0){
				request.append(SPLIT);
			}
			request.append(FORMAT);
			request.append(format);
			fieldsAdded++;
		}
		
		public void setTitles(String titles){
			if (fieldsAdded > 0){
				request.append(SPLIT);
			}
			request.append(TITLES);
			request.append(titles);
			fieldsAdded++;
		}
		
	}
}
